﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

using BawangMemo.BackendModels;

namespace BawangMemo.BawangMemoView.View
{
    /// <summary>
    /// ReviewView.xaml 的交互逻辑
    /// </summary>
    public partial class ReviewView : UserControl
    {
        private int nowIndex = 0;
        private List<WordWrapper> reviewList;
        private Window container;
        private double score;

        public ReviewView()
        {
            InitializeComponent();
        }

        public ReviewView(List<WordWrapper> reviewList, Window container)
            : this()
        {
            //this.reviewList = new List<WordWrapper>
            Random random = new Random((int) (DateTime.Now.ToBinary() & 0x7fffffff));
            WordWrapper[] temp = new WordWrapper[reviewList.Count];
            int[] index = new int[reviewList.Count];
            for (int i = 0; i < index.Length; i++) index[i] = i;
            for (int i = index.Length - 1; i > 0; i--)
            {
                int j = random.Next(0, i + 1);
                int swap = index[i];
                index[i] = index[j];
                index[j] = swap;
            }
            for (int i = 0; i < index.Length; i++)
                temp[i] = reviewList[index[i]];
            this.reviewList = temp.ToList<WordWrapper>();

            this.container = container;
            wordView1.SetWord(this.reviewList[nowIndex]);
        }

        private void UpdateProgressBar(ProgressBar progressBar1)
        {
            progressBar1.Value = (nowIndex * 100.0) / reviewList.Count;
        }

        private void ShowAfterButtonClicked()
        {
            wordView1.ToggleExplanations(wordView1.Contents.Keys.ToArray<string>(), true);
        }

        private void button_yes_Click(object sender, RoutedEventArgs e)
        {
            button_yes.Visibility = Visibility.Hidden;
            button_no.Visibility = Visibility.Hidden;
            button_amb.Visibility = Visibility.Hidden;
            button4.Visibility = Visibility.Visible;
            button5.Visibility = Visibility.Visible;
            ShowAfterButtonClicked();
        }

        private void button4_Click(object sender, RoutedEventArgs e)
        {
            button_yes.Visibility = Visibility.Visible;
            button_no.Visibility = Visibility.Visible;
            button_amb.Visibility = Visibility.Visible;
            button4.Visibility = Visibility.Hidden;
            button5.Visibility = Visibility.Hidden;
            // user response 3
            //course.UserResponse(wordView1.GetWord(), 3);
            score = (score * nowIndex + 100) / (nowIndex + 1);
            wordView1.GetWord().ChangeScore(3);
            // Go to next word
            TryNext();
        }

        private void button5_Click(object sender, RoutedEventArgs e)
        {
            button_yes.Visibility = Visibility.Visible;
            button_no.Visibility = Visibility.Visible;
            button_amb.Visibility = Visibility.Visible;
            button4.Visibility = Visibility.Hidden;
            button5.Visibility = Visibility.Hidden;
            // user response 1
            //course.UserResponse(wordView1.GetWord(), 1);
            score = (score * nowIndex + 50) / (nowIndex + 1);
            wordView1.GetWord().ChangeScore(1);
            // Go to next word
            TryNext();
        }

        private void button_amb_Click(object sender, RoutedEventArgs e)
        {
            button_yes.Visibility = Visibility.Hidden;
            button_no.Visibility = Visibility.Hidden;
            button_amb.Visibility = Visibility.Hidden;
            button_go.Visibility = Visibility.Visible;
            // user response 2
            //course.UserResponse(wordView1.GetWord(), 2);
            score = (score * nowIndex + 75) / (nowIndex + 1);
            wordView1.GetWord().ChangeScore(2);

            ShowAfterButtonClicked();
        }

        private void button_no_Click(object sender, RoutedEventArgs e)
        {
            button_yes.Visibility = Visibility.Hidden;
            button_no.Visibility = Visibility.Hidden;
            button_amb.Visibility = Visibility.Hidden;
            button_go.Visibility = Visibility.Visible;
            // user response 0
            //course.UserResponse(wordView1.GetWord(), 0);
            score = (score * nowIndex + 25) / (nowIndex + 1);
            wordView1.GetWord().ChangeScore(0);

            ShowAfterButtonClicked();
        }

        private void button_go_Click(object sender, RoutedEventArgs e)
        {
            button_yes.Visibility = Visibility.Visible;
            button_no.Visibility = Visibility.Visible;
            button_amb.Visibility = Visibility.Visible;
            button4.Visibility = Visibility.Hidden;
            button5.Visibility = Visibility.Hidden;
            // Go to next word
            TryNext();
        }

        private void TryNext()
        {
            button_go.Visibility = Visibility.Hidden;
            ++nowIndex;
            UpdateProgressBar(progressBar1);
            if (nowIndex < reviewList.Count)
            {
                wordView1.SetWord(reviewList[nowIndex]);
            }
            else
            {
                MessageBox.Show(String.Format("最近{0}个单词复习完毕，得分：{1}", reviewList.Count,
                    this.Score));
                //((Grid)this.Parent).Children.Clear();
                ((Window)((Grid)this.Parent).Parent).Close();
            }
        }

        private void wordView1_TagEventHandler(object sender, RoutedEventArgs e)
        {
            TagEventArgs args = (TagEventArgs)e;
            //DEBUG: wintokk
            WordWrapper word = wordView1.GetWord();
            //MessageBox.Show(word.Id + " " + word.Spell + " " + args.Tag);
            if (args.IsNew)
                word.AddTag(args.Tag);
            else
                word.RemoveTag(args.Tag);

            wordView1.Tags = TagRepository.ListWordTags(word);
        }

        public double Score
        {
            get
            {
                return score;
            }
        }
    }
}
